package nc.bs.pub.action;

import java.util.List;

import nc.bs.common.projectFAUtils;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.itf.ct.saledaily.ISaledailyApprove;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.ct.saledaily.entity.AggCtSaleVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pubapp.pub.power.PowerActionEnum;
import nc.vo.scmpub.res.billtype.CTBillType;
import nc.vo.uap.pf.PFBusinessException;
import nccloud.vo.scmpub.utils.power.DataPermissionUtil;

public class N_Z3_SAVE extends AbstractCompiler2 {
	public String getCodeRemark() {
		return "    nc.vo.ct.saledaily.entity.AggCtSaleVO inObject  =(nc.vo.ct.saledaily.entity.AggCtSaleVO) ();\n     setParameter(\"INVO\", inObject);\n return null;\n";
	}

	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			super.m_tmpVo = vo;
			Object retValue = null;
			AggCtSaleVO[] vos = (AggCtSaleVO[]) ((AggCtSaleVO[]) this.getVos());
			if (vo.isCloudEntry) {
				DataPermissionUtil.checkPermission(vos, CTBillType.SaleDaily.getCode(),
						PowerActionEnum.COMMIT.getActioncode(), "vbillcode");
			}

			//lhw 231128 提交前校验项目 begin
			new projectFAUtils().projectDataApprove("ct_sale", vos, "commit");
			//end
			
			//humf,保存校验,没有附件不允许提交
			for(int y = 0;y<vos.length;y++) {
				String sql ="SELECT * FROM (select substr(filepath, INSTR(filepath, '/', 1, lengthb(regexp_replace(filepath, '[^/]', null))) - 20, 20) AS pk_bill from sm_pub_filesystem sm where ISFOLDER = 'n') pk WHERE  pk.pk_bill='"+vos[y].getParentVO().getPk_ct_sale()+"'  ";
	            List<String> messages=(List<String>) new BaseDAO().executeQuery(sql, new ColumnListProcessor());				
				if(messages.isEmpty() || messages.size()==0) {//有附件
					throw new BusinessException("销售合同："+vos[y].getParentVO().getVbillcode()+"无附件不允许提交");
				}
			}
			//end
			
			retValue = ((ISaledailyApprove) NCLocator.getInstance().lookup(ISaledailyApprove.class)).sendapprove(vos,
					this);
			return retValue;
		} catch (Exception var4) {
			if (var4 instanceof BusinessException) {
				throw (BusinessException) var4;
			} else {
				throw new PFBusinessException(var4.getMessage(), var4);
			}
		}
	}
}